package com.lili.operate.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.common.result.Result;
import com.lili.model.operate.Article;
import com.lili.model.operate.ArticleCategory;
import com.lili.operate.service.ArticleCategoryService;
import com.lili.operate.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Api(tags = "文章管理")
public class ArticleController {
    @Autowired
    ArticleCategoryService articleCategoryService;


    @Autowired
    ArticleService articleService;

    /**
     * 新增文章
     * @param article
     * @return
     */
    @ApiOperation("新增文章")
    @PostMapping("/save/article")
    public Result saveArticle(@RequestBody Article article){
        int rs = articleService.saveArticle(article);
        return rs==1?Result.ok():Result.fail();
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @DeleteMapping("/delete/article")
    public Result deleteArticle(long id){
        int rs = articleService.deleteArticle(id);
        return rs==1?Result.ok():Result.fail();
    }

    /**
     * 更新文章
     * @param article
     * @return
     */
    @PutMapping("/update/article")
    public Result updateArticle(@RequestBody Article article){
        int rs = articleService.updateArticle(article);
        return rs==1?Result.ok():Result.fail();
    }


    /**
     * 更新文章状态
     * @param map
     * @return
     */
    @PutMapping("/update/status")
    public Result statusArticle(@RequestBody Map map){
        long id = Long.parseLong(map.get("id").toString());
        Byte status = Byte.valueOf(map.get("status").toString());
        int rs = articleService.statusArticle(id,status);
        return rs==1?Result.ok():Result.fail();
    }

    /**
     * 查询所有的文章
     * @param map
     * @return
     */
    @PostMapping("/query/article")
    public Result queryArticles(@RequestBody Map map){
        Page<Article> page = articleService.queryArticles(map);
        return page==null?Result.fail():Result.ok(page);
    }


    /**
     * 新增文章分类
     * @param articleCategory
     * @return
     */
    @PostMapping("/save/article/category")
    public Result saveArticleCategory(@RequestBody ArticleCategory articleCategory){
        int rs = articleCategoryService.saveArticleCategory(articleCategory);
        return rs==1?Result.ok():Result.fail();
    }

    /**
     * 查询所有的文章类别及其子类
     * @return
     */
    @GetMapping("/query/article/category")
    public Result queryArticleCategory(){
        List<Map> list = articleCategoryService.queryArticleCategory();
        return list.size()==0?Result.fail():Result.ok(list);
    }


    /**
     * 根据id更新文章分类类别
     * @return
     */
    @PutMapping("/update/article/category")
    public Result updateArticleCategory(ArticleCategory articleCategory){
        int rs = articleCategoryService.updateArticleCategory(articleCategory);
        return rs==1?Result.ok():Result.fail();
    }


    /**
     * 根据id 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/delete/article/category")
    public Result deleteArticleCategory(long id){
        int rs = articleCategoryService.deleteArticleCategory(id);
        return rs==1?Result.ok():Result.fail();
    }
}
